package com.mxchip.livestarmobile.mobile.db.dao

import androidx.lifecycle.LiveData
import androidx.room.*
import com.mxchip.livestarmobile.mobile.db.entity.Account

@Dao
interface AccountDao {

    @Query("SELECT * FROM Account order by t_created desc")
    fun getAll(): LiveData<List<Account>>

    @Query("SELECT * FROM Account WHERE uid IN (:ids)")
    fun loadAllByIds(ids: LongArray): LiveData<List<Account>>

    @Query("SELECT * FROM Account WHERE uid = (:id)")
    fun loadAllById(id: Long): LiveData<Account>

    @Query("SELECT * FROM Account WHERE user_id = (:userId)")
    fun loadByUserId(userId: String): LiveData<Account>

    @Query("SELECT * FROM Account WHERE phone_number = (:phoneNumber)")
    fun loadByPhoneNumber(phoneNumber: String): LiveData<Account>

    @Insert
    suspend fun insertAll(vararg accounts: Account)

    @Delete
    suspend fun delete(account: Account)

    @Update
    suspend fun update(vararg accounts: Account)


    @Insert(onConflict = OnConflictStrategy.IGNORE)
    suspend fun insertIgnore(entity: Account) : Long

    @Update
    suspend fun update(entity: Account)

    @Transaction
    suspend fun insertOrUpdate(entity: Account) {
        if (insertIgnore(entity) == -1L) {
            update(entity)
        }
    }
}